package Account.readXML;
import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class ReadAndPrintXMLFile{

    public static void main (String argv []){
    try {

            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse (new File("conf/interfaceMessage.xml"));

            // normalize text representation           
            doc.getDocumentElement ().normalize ();
            System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName());


            NodeList listOfPersons = doc.getElementsByTagName("system");
            int totalPersons = listOfPersons.getLength();
            System.out.println("Total no of people : " + totalPersons);

            for(int s=0; s<listOfPersons.getLength() ; s++){
                Node firstPersonNode = listOfPersons.item(s);
                if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){
                    Element firstPersonElement = (Element)firstPersonNode;
                    //-------                   
                   NodeList firstNameList = firstPersonElement.getElementsByTagName("systemType");
                   for(int i=0;i<firstNameList.getLength();i++){
                	   Element firstNameElement = (Element)firstNameList.item(i);
                	   NodeList textFNList = firstNameElement.getChildNodes();
                		System.out.println(((Node)textFNList.item(i)).getFirstChild().getNodeValue().trim());
                		
                   }
//                    Element firstNameElement = (Element)firstNameList.item(0);
//
//                    NodeList textFNList = firstNameElement.getChildNodes();
//                    for(int i=0;i<textFNList.getLength();i++){
//                    	System.out.println(((Node)textFNList.item(i)).getNodeValue().trim());
//                    }
//                    System.out.println("First Name : " +
//                           ((Node)textFNList.item(0)).getNodeValue().trim());
                }//end of if clause

            }//end of for loop with s var

        }
        catch (SAXParseException err) {
               System.out.println ("** Parsing error" + ", line "
                                          + err.getLineNumber () + ", uri " + err.getSystemId ());
               System.out.println(" " + err.getMessage ());

        }
        catch (SAXException e) {
                Exception x = e.getException ();
                ((x == null) ? e : x).printStackTrace ();

        }
        catch (Throwable t) {
                t.printStackTrace ();
        }
    }

}